import 'package:flutter/material.dart';
import 'package:ibooks/const/constant.dart';
import 'package:ibooks/route/custom_page_route.dart';
import 'package:ibooks/service/system.dart';
import 'package:ibooks/utils/utils.dart';

import '../model/index.dart';
import '../pages/index.dart';

class BookItem extends StatefulWidget {
  final Book book;

  final bool isSelected;
  final bool isChecked;

  final VoidCallback callback;

  BookItem({this.book, this.isSelected, this.isChecked = false, this.callback});

  @override
  _BookItemState createState() => _BookItemState();
}

class _BookItemState extends State<BookItem> {
  bool isChecked = false;

  final SystemService _service = new SystemService();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        margin: EdgeInsets.all(5),
        padding: EdgeInsets.fromLTRB(5, 10, 0, 0),
        decoration: BoxDecoration(
            color: Color.fromRGBO(68, 75, 80, 1),
            boxShadow: [
              BoxShadow(
                  color: Colors.grey[400].withOpacity(0.15),
                  blurRadius: 3,
                  offset: Offset(3, 4))
            ],
            borderRadius: BorderRadius.circular(4)),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: getBookView(),
        ),
      ),
      onTap: () {
        if (widget.isSelected || !isChecked) {
          selectBook(widget.isChecked);
        } else {
          loadBook();
        }
      },
      onLongPress: widget.callback,
    );
  }

  loadBook() {
    if (widget.book.type != FileType.TEXT && widget.book.type != FileType.TXT) {
      print("对不起，该格式暂时不支持");
      return;
    }
    Navigator.push(
        context,
        RightSliderPageRoute(ReaderPage(
          book: widget.book,
        )));
  }

  getBookView() {
    List<Widget> stackView = [];
    stackView.add(Text(
      widget.book.name,
      maxLines: 1,
      style: TextStyle(color: Colors.white),
      overflow: TextOverflow.ellipsis,
    ));
    if (widget.isSelected) {
      stackView.add(Expanded(
          child: Container(
        alignment: Alignment.bottomRight,
//        color: Colors.red,
        child: Container(
            constraints: BoxConstraints(maxWidth: 17, maxHeight: 17),
            color: Colors.white,
            child: Checkbox(
              value: isChecked,
              checkColor: Colors.black,
              activeColor: Colors.white,
              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
              onChanged: selectBook,
            )),
      )));
    }
    return stackView;
  }

  selectBook(bool checked) {
    if (checked) {
      var list = _service.getStringList(Constant.selectBookIds) ?? [];
      var selectId = widget.book.id.toString();
      if (!list.contains(selectId)) {
        list.add(selectId);
      }
      print("select book ids ===> $list}");
      _service.setStringList(Constant.selectBookIds, list);
    }
    setState(() {
      isChecked = checked;
    });
  }
}
